package user

import (
	"context"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/tools/ent"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogsDao struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLoginLogsDao(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogsDao {
	return &UserLoginLogsDao{
		svcCtx: svcCtx,
		ctx:    context.Background(),
		Logger: logx.WithContext(ctx),
	}
}

// CreateUserLoginLog -创建用户登录日志
func (l *UserLoginLogsDao) CreateUserLoginLog(req *ent.HzUserLoginLogs, tx *ent.Tx) error {
	client := l.svcCtx.Orm.HzUserLoginLogs
	if tx != nil {
		client = tx.HzUserLoginLogs
	}

	// 定义时间（避免秒差）
	unix := time.Now().Unix()

	// 执行添加
	err := client.Create().
		SetUserID(req.UserID).
		SetLoginType(req.LoginType).
		SetDeviceType(req.DeviceType).
		SetEquipmentID(req.EquipmentID).
		SetIPAddress(req.IPAddress).
		SetLoginTime(unix).
		SetCreatedAt(unix).
		SetUpdateAt(unix).
		Exec(l.ctx)
	if err != nil {
		return err
	}
	return nil
}
